<?php


namespace Yj\Apidog;

use App\Controller\Admin\BannerController;
use App\Dao\System\SystemRouteDao;
use App\Model\System\SystemRoute;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Mapping;
use Hyperf\HttpServer\Router\DispatcherFactory as HyperfDispatcherFactory;
use Hyperf\HttpServer\Router\RouteCollector;
use Hyperf\HttpServer\Router\Router;
use Yj\Apidog\Annotation\ApiController;

class DispatcherFactory extends HyperfDispatcherFactory
{


    protected function handleController(string $className, Controller $annotation, array $methodMetadata, array $middlewares = []): void
    {
        if (!$methodMetadata) {
            return;
        }

        $route_prefix = $annotation->route_prefix;


        $router = $this->getRouter($annotation->server);


        $prefix = $this->getPrefix($className, $annotation->prefix);

//        dump($methodMetadata);

        foreach ($methodMetadata as $methodName => $values) {
            $methodMiddlewares = $middlewares;

            if (isset($values)) {
                $methodMiddlewares = array_merge($methodMiddlewares, $this->handleMiddleware($values));
                $methodMiddlewares = array_unique($methodMiddlewares);
            }

            foreach ($values as $mapping) {
                if (!($mapping instanceof Mapping)) {
                    continue;
                }
                if (!isset($mapping->methods)) {
                    continue;
                }
                $path = $mapping->path;
                if ($path === '') {
                    $path = $prefix;
                } elseif ($path[0] !== '/') {
                    $path = $prefix . '/' . $path;
                }
                dump($path);


                $router->addRoute($mapping->methods, $path, [$className, $methodName], [
                    'middleware' => $methodMiddlewares,
                ]);


                if (isset($mapping->route) && $mapping->route) {
                    dump($className,$methodName,$mapping->methods);
                    //添加auth
                    App(SystemRouteDao::class)->sync($route_prefix . ':' . $mapping->route, $path, $mapping->description,$className,$methodName,$mapping->methods);
                }
            }
        }
    }

    public function initConfigRoute()
    {
//        collect($this->routers)->each(function (RouteCollector $route,$servername){
//                collect($route->getData())->each(function ($data)use($servername){
//                        collect($data)->each(function ($routes,$method)use($servername){
//                            collect($routes)->each(function ($route)use($method,$servername){
//                                dd($route,$servername,$method);
//                            });
//                        });
//                });
//        });
        parent::initConfigRoute();
    }


    protected function initAnnotationRoute(array $collector): void
    {

        foreach ($collector as $className => $metadata) {
            if (isset($metadata['_c'][ApiController::class])) {
                $middlewares = $this->handleMiddleware($metadata['_c']);
                $this->handleController($className, $metadata['_c'][ApiController::class], $metadata['_m'] ?? [], $middlewares);
            }
        }

        parent::initAnnotationRoute($collector);
    }
}